package cn.endeavour.product.service.impl;


import cn.endeavour.basic.service.impl.BaseServiceImpl;
import cn.endeavour.product.domain.Product;
import cn.endeavour.product.domain.ProductDetail;
import cn.endeavour.product.mapper.ProductDetailMapper;
import cn.endeavour.product.mapper.ProductMapper;
import cn.endeavour.product.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Override
    public void onSale(List<Long> ids) {
        productMapper.onSale(ids);
    }

    @Override
    public void offSale(List<Long> ids) {
        productMapper.offSale(ids);
    }

    @Override
    @Transactional
    public void add(Product product) {
        // 先保存product，返回主键id，在保存细节
        productMapper.save(product);
        ProductDetail detail = product.getDetail();
        detail.setProduct_id(product.getId());
        productDetailMapper.save(detail);
    }

    @Override
    @Transactional
    public void update(Product product) {
        productMapper.update(product);
        productDetailMapper.update(product.getDetail());
    }

    @Override
    @Transactional
    public void delete(Long id) {
        productDetailMapper.remove(id);
        productMapper.remove(id);
    }

    @Override
    @Transactional
    public void patchDelete(List<Long> ids) {
        productDetailMapper.patchRemove(ids);
        productMapper.patchRemove(ids);
    }

}
